package com.sky.mapper;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.vo.Top10;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from sky_take_out.orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    List<Orders> selectPage(Integer status, Long userId);


    @Select("select * from sky_take_out.orders where id=#{id};")
    Orders getById(Long id);



    Page<Orders> selectPageAdmin(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select count(*)  from sky_take_out.orders where status =#{TO_BE_CONFIRMED};")
    Integer getNumbertoBeConfirmed(Integer TO_BE_CONFIRMED);

    @Select("select count(*)  from sky_take_out.orders where status =#{CONFIRMED};")
    Integer getNumberconfirmed(Integer CONFIRMED);

    @Select("select count(*)  from sky_take_out.orders where status =#{DELIVERY_IN_PROGRESS};")
    Integer getNumberdeliveryInProgress(Integer DELIVERY_IN_PROGRESS);

    @Update("update sky_take_out.orders set status=3 where id=#{id}")
    void confirmOrder(Long id);



    void rejectionOrder(OrdersRejectionDTO ordersRejectionDTO);


    Orders selectById(Long id);


    @Select("select * from sky_take_out.orders where status=#{status} and order_time<#{times};")
    List<Orders> getByStatusAndOrdertimeLT(Integer status, LocalDateTime times);

    @Select("select sum(amount) from sky_take_out.orders where order_time>=#{begin} and order_time<=#{end} and status=#{completed};")
    Double sumAmount(LocalDateTime begin, LocalDateTime end, Integer completed);

    @Select("select count(*) from sky_take_out.orders where order_time>=#{begin} and order_time<=#{end};")
    Integer selectCount(LocalDateTime begin, LocalDateTime end);


    @Select("select count(*) from sky_take_out.orders where order_time>=#{begin} and order_time<=#{end} and status=#{completed};")
    Integer selectvalidCount(LocalDateTime begin, LocalDateTime end, Integer completed);

    @Select("select count(*) from sky_take_out.orders;")
    Integer selectCountAll();

    @Select("select count(*) from sky_take_out.orders where status=#{completed};")
    Integer selectvalidCountAll(Integer completed);

    @Select("select od.name,sum(od.number) num \n" +
            "from sky_take_out.order_detail od,sky_take_out.orders o where od.order_id=o.id and status=5 and order_time>=#{beginTime} and order_time<=#{endTime}\n" +
            "group by od.name\n" +
            "order by sum(od.number) desc\n" +
            "limit 0,10")
    List<Top10> selectTop10(LocalDate beginTime, LocalDate endTime);


    Double sumByMap(Map map);

    Integer countByMap(Map map);
}
